package com.fenghao.subject.domain.handler.subject;

import com.fenghao.subject.common.enums.SubjectInfoTypeEnum;
import com.fenghao.subject.domain.convert.RadioSubjectConvert;
import com.fenghao.subject.domain.entity.SubjectAnswerBO;
import com.fenghao.subject.domain.entity.SubjectInfoBO;
import com.fenghao.subject.infra.basic.entity.SubjectRadio;
import com.fenghao.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 单选题目的策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override

    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    /**
     * 单选题目的插入
     */
    @Override
    public void addSubject(SubjectInfoBO subjectInfoBO) {

        List<SubjectAnswerBO> subjectAnswerBoList = subjectInfoBO.getOptionsList();
        List<SubjectRadio> subjectRadioList = RadioSubjectConvert
                .INSTANCE.convertSubjectAnswerBoListToSubjectRadioList(subjectAnswerBoList);
        subjectRadioList.forEach(subjectRadio -> subjectRadio.setSubjectId(subjectInfoBO.getId()));
        // 批量插入
        subjectRadioService.insertBatch(subjectRadioList);
    }
}
